using System.Collections.Generic;
using System.Linq;
using Kevin.Comm.Utility;
using Kevin.Shop.Business.Service.Entity.Helper;
using Kevin.Shop.Business.Service.Entity.Products.Product;
using Kevin.Shop.Business.Service.Entity.SalesManagement.Enumerations;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.Common.Helper;
using RiderShop.Mobile.ServiceEntity.Product;
using RiderShop.Mobile.ServiceEntity.Sort;

namespace RiderShop.Mobile.Modle.Home
{
    public class HonePromotionViewModel
    {
        public HonePromotionViewModel(int tekNumber = 1)
        {
            if (!KevinCache.Exists(CacheKeys.CacheKeyPromotionProducts))
            {
                Porducts = RestfulHelper.Query<PromotionProductExtend>(SiteUrl.FormatUrl("ProductManagement_HomePageDataSource"));
                KevinCache.Insert(CacheKeys.CacheKeyPromotionProducts, Porducts,60*60);
            }
            else
            {
                Porducts = KevinCache.Get(CacheKeys.CacheKeyPromotionProducts) as List<PromotionProductExtend>;
            }

            if (groups == null)
            {
                groups = new List<PromotionCategoryExtend>();
            }
            PromotionCategoryExtend category = null;
            List<PromotionProductExtend> groupList = null;
            if (Porducts != null)
            {
                groupList = Porducts.Where(p => (p.Type == WebsitePromotionType.SoldQtyByGroup.ToString() || p.Type == WebsitePromotionType.LatestActive.ToString() 
                                                || p.Type == WebsitePromotionType.GroupProduct.ToString())).ToList();
                if (WebClientContext.Current.CategorySwitch == WebClientContext.StrGroup)
                {
                    foreach (var item in groupList)
                    {
                        if (!groups.Exists(p => (p.GroupId == item.GroupId)))
                        {
                            category = new PromotionCategoryExtend() { GroupId = item.GroupId, GroupName = item.GroupName, CategoryId = item.CategoryId, CategoryName = item.CategoryName };
                            category.Products = UtilityTools.RandomSort(Porducts.Where(p => (p.GroupId == item.GroupId)).ToArray()).Take(tekNumber).ToList();
                            groups.Add(category);
                        }
                    }
                }
                else
                {
                    foreach (var item in groupList)
                    {
                        if (!groups.Exists(p => (p.CategoryId == item.CategoryId)))
                        {
                            category = new PromotionCategoryExtend() { GroupId = item.GroupId, GroupName = item.GroupName, CategoryId = item.CategoryId, CategoryName = item.CategoryName };
                            category.Products = UtilityTools.RandomSort(Porducts.Where(p => (p.CategoryId == item.CategoryId)).ToArray()).Take(tekNumber).ToList();
                            groups.Add(category);
                        }
                    }
                }
            }
        }

        public List<PromotionProductExtend> Porducts;

        private List<PromotionCategoryExtend> groups;

        public List<PromotionCategoryExtend> GroupList
        {
            get {  return groups; }
        }

        public int PromotionItemCount
        {
             get
             {
                 var count = 0;
                 foreach (var category in GroupList)
                 {
                     foreach (var product in category.Products)
                     {
                         count++;
                     }
                 }
                 return count;
             }
        }
    }
}